import router from '../router/index'
import { useLogin } from '../store/cart'
import { usePermission } from '../store/permission'
import NProgress from 'nprogress'
// import routes from '../utills/autoRouter'
router.beforeEach(async (to, from) => {
    //打开进度条
    NProgress.start()
    let permissionStore = usePermission()
    // console.log(permissionStore.menulsit);
    let token = sessionStorage.getItem('qf-token')
    if (!token) {
        if (to.path !== '/login') {
            // await permissionStore.fetch_menulist()
            return {
                path: '/login'
            }
        }
    } else {
        //如果menulist的长度位0才调用方法发起请求
        if (permissionStore.menulsit.length === 0) {
            //调用usePermission中的action方法发送请求
            await permissionStore.fetch_menulist()
            // console.log(123,to);
            return {
                ...to
            }
        }
    }
})
router.afterEach(async(to, from) => {
    // console.log(456,to);
    // let token = sessionStorage.getItem('qf-token')
    // if(token){
    //     let permissionStore = usePermission()
    //     await permissionStore.fetch_menulist()
    // }
    // console.log(routes);
    // let routess = routes
    
    //关闭进度条
    NProgress.done()
    let breadRoute = useLogin()
    // console.log(123);
    breadRoute.$patch((state) => {
        state.bread = to.matched
    })
})


